<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="shortcut icon" href="../images/yc.png" type="image/x-icon">
<link rel="stylesheet" href="../css/layui.css" media="all">
</head>
<body>
<br />
<div class="demoTable">
<form id="myform">
	<span class="navy_padding">法规搜索:</span>
    <div class="layui-inline">
      <input class="layui-input" name="searchContent" id="searchContent" style="border:1px solid grey" autocomplete="on" placeholder="请输入你想搜索的公告...">
    </div>
<!--    <div class="layui-inline">-->
<!--      <input type="file" name="photo" id="photo" onchange="setImagePreviews(this,'showpic')">-->
<!--    </div>-->
    <button type="button" class="layui-btn layui-btn-warm layui-btn-radius" onclick="search()"> 搜索  </button>
   </form>
</div>
<div id="showpic" style="width:100%;height:100px"></div>

<table class="layui-hide" id="tender_data" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="goto">跳转</a>
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<!--	<a class="layui-btn layui-btn-xs" lay-event="hidden">隐藏</a>-->
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="checkboxTpl">
  <input type="checkbox" name="lock" value="{{d.tno}}" title="在架" lay-filter="lockDemo" {{ d.status == 2 ? 'checked' : '' }}>
</script>

<script src="../js/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../js/layui.all.js" charset="utf-8"></script>
<script src="../js/ajaxfileupload.js" charset="utf-8"></script>
<script src="../js/show-dialog.js" charset="utf-8"></script>
<script src="../js/showpic.js" charset="utf-8"></script>
 
<script>

//搜索公告
function search(){
	let searchContent = $.trim($("#searchContent").val());
	let url = ""
	if(!searchContent){
		url = '/tender/searchTenders'
	}else{
		url = '/tender/searchTenders'
	}
	
	layui.use('table', function(){
		var table = layui.table, form = layui.form;
		table.render({
	    	elem: '#tender_data',
			url:'/tender/searchTenders',
			toolbar: 'toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
	    	title: '管理员信息表',
	    	cellMinWidth: 120,
	    	id: "admin_data",
	    	limit:10000,
	    	where:{
	    		searchTitle:searchContent,
	    		searchContent:searchContent,
	    		tenderFlag:1,
                tno:3
			},
	    	method:"POST",
	    	cols: [[
	  	      /*{
	  			type: 'checkbox', fixed: 'center'},*/
	  			{field:'title', title:'标题', align: 'center'},
	  			{field:'createTime', title:'创建时间', align: 'center'},
                {field:'createDate', title:'发布时间', align: 'center'},
                {field:'viewCount', title:'浏览量', align: 'center'},
	  			{field:'status', title:'当前状态', align: 'center',templet: stateFormat},
	  			{fixed: 'right', title:'操作', toolbar: '#barDemo', align: 'center'},
	  			//{field:'status', title:'是否上架', width:110, templet: '#checkboxTpl', unresize: true}
	  	    ]]
		});
	})
	
	/*
	$.ajaxFileUpload({
		url:"/tender/searchTenders",
		secureuri:false,
		fileElementId:"photo",
		data:{searchTitle:searchContent,searchContent:searchContent},
		dataType:"json",
		success:function(rt,status){
			if(rt.code == 700){
				showMsg("当前登录状态,但是请先登录","red",function(){
					location.href="../login.html"
				})
			}
			
			if(){
				$("#myform")[0].reset();   //重置表单
				$("#showpic").html("");  //清空图片预览
				layui.table.reload("tender_data");   //重新加载表格数据
				return;
			}
			console.log(rt)
			layer.msg("未查询到数据..." + rt.msg,{icon:2,time:2000});
		},
		error:function(rt,status,e){
			layer.msg("查找失败\n" + e,{icon:2,time:2000})
		}
	})*/
	
}

layui.use('table', function(){
	var table = layui.table, form = layui.form;
	table.render({
    	elem: '#tender_data',
		url:'/tender/searchTendersByTno',
		//toolbar: 'default', //开启头部工具栏，并为其绑定左侧模板
		limit:10,
		limits:[10,20,30,50,100],
		where:{
    		tenderFlag:1,
            tno:4
		},
    	title: '法律法规查询表',
    	cellMinWidth: 120,
    	id: "tender_data",
    	method: "POST",
	    cols: [[
	      /*{
			type: 'checkbox', fixed: 'center'},*/
			{field:'title', title:'标题', align: 'center'},
			{field:'createTime', title:'创建时间', align: 'center'},
            {field:'createDate', title:'发布时间', align: 'center'},
            {field:'viewCount', title:'浏览量', align: 'center'},
			{fixed: 'right', title:'操作', toolbar: '#barDemo', align: 'center'},
			//{field:'status', title:'是否上架', width:110, templet: '#checkboxTpl', unresize: true}
	    ]],
		page: { 
			layout: ['prev', 'page', 'next', 'count', 'skip', 'limit'], //自定义分页布局
	      //,curr: 5 // 设定初始在第 5 页
	      groups: 10, // 只显示 1 个连续页码
	      first: '首页', //不显示首页
	      last: '尾页' //不显示尾页
	      
	    },
	    done(res){
	    	console.log(res)
	    }
	});
	
	//监听锁定操作
	//监听锁定操作
	form.on('checkbox(lockDemo)', function(obj){
		obj.elem.checked = !obj.elem.checked;
		form.render('checkbox'); // 渲染
		let txt = "下架";
		let status = 1;
		if (!obj.elem.checked) {
			txt = "上架";
			status = 2;
		}
		layer.confirm('您确定要 ' + txt + ' 这个类型的商品吗?', {btn: ['确定','取消']}, function(){
			$.post("../../type/updateStatus",{tno:obj.elem.value,status:status},rt=>{
				if(rt == 1){
					layer.closeAll('dialog');  //关闭提示框
					obj.elem.checked =  !obj.elem.checked;  //改变按钮状态
					form.render('checkbox');  //渲染
					return;
				}
				layer.alert(txt + "  失败，请稍后重试...",{title:"失败提示"});
			});
		});
	});

  
	// 监听行工具事件
	table.on('tool', function(obj){
        var data = obj.data;
        let newName = '';
        if(obj.event === 'del'){
            layer.confirm('数据一旦删除将不能恢复，您确定要删除吗？', function(index){
                $.post("/tender/delete",{id:data.id,tno:3},rt=>{
                    if(rt.code == 200){
                        obj.del();
                        layer.close(index);
                    }
                },"json");
            });
    	} else if(obj.event === 'edit'){
            sessionStorage.setItem("upId",data.id)
            location.href='edit.html'
		}else if(obj.event === 'hidden'){
      		layer.prompt({
      			title:'修改名称',
        		formType: 3,
        		value: newName
      		}, function(value, index){
				obj.update({
					newName: value
        		});
				this.newName = value;
				// 更新商品类型名
        		
        		layer.close(index);
      		});
		}else if(obj.event === 'goto'){
            console.log(data)
            sessionStorage.setItem("id",data.id)
            sessionStorage.setItem("bdNo",data.bdNo)
            window.open("../../front/single.html?" + data.id)
        }
	});
});

//对状态值格式化
function stateFormat(d){
	let status = d.status
	if(status == "1"){
		return '<span style="color:red">新发布</span>'
	}else if(status == "2"){
		return '<span style="color:green">已中标</span>'
	}else{
		return '<span style="color:grey">其他</span>'
	}
}


function showPic(t,tno) {
    var t = $(t).find("img");
    if(t == null||t == ''){
        return;
    }
    
    layer.open({
        type: 1,
        title:'图片',
        skin: 'layui-layer-rim', //加上边框
        area: ['75%', '85%'], //宽高
        shadeClose: true, //开启遮罩关闭
        end: function (index, layero) {
            return false;
        },
        content: '<div style="text-align:center"><img src="' + $(t).attr('src') + '"  style="width:75%;height:81%" /></div><div style="margin-left:550px"><span style="color:red;width:30px">更换图片</span><input type="file" name="photoC" id="photoC"><button onclick="change('+ tno +')">保存</button></div>'
    });
}

function change(tno){
	console.log(tno)
	let fileList = document.getElementById("photoC").files;
	if(fileList.length <= 0){
		layer.msg("请选择图片...");
		return;
	}
	console.log(fileList)
	
	$.ajaxFileUpload({
		url:"../../type/updateImage",
		secureuri:false,
		fileElementId:"photoC",
		data:{tno:tno},
		dataType:"json",
		success:function(rt,status){
			if(rt.code == 700){
				showMsg("虽然我不知道你怎么进来的,但是请先登录","red",function(){
					location.href=""
				})
			}
			
			if(rt.code == 200){
				$("#myform")[0].reset();   //重置表单
				$("#showpic").html("");  //清空图片预览
				layer.msg("修改图片成功...",{icon:1,time:2000});
				layui.table.reload("tender_data");   //重新加载表格数据
				return;
			}
			layer.msg("修改图片失败...",{icon:2,time:2000});
		},
		error:function(rt,status,e){
			layer.msg("修改图片失败\n" + e,{icon:2,time:2000})
		}
	});
	console.log()
}
</script>
</body>
</html>